package com.tong.wms.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tong.wms.config.QueryPageParam;
import com.tong.wms.config.Result;
import com.tong.wms.pojo.Storage;
import com.tong.wms.service.StorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/storage")
public class StorageController {
    @Autowired
    private StorageService storageService;

    @GetMapping("/list")
    public Result list() {
        List<Storage> list = storageService.list();
        return Result.success(list);
    }

    //物品管理需要的仓库数据
//    @GetMapping("/list")
//    public Result list(@RequestParam int id){
//        List<Storage> list = storageService.list();
//        return Result.success(list);
//    }

    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query) {
        HashMap param = query.getParam();
        String name = (String) param.get("name");

        Page<Storage> page = new Page<>();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<Storage> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(name) && !"null".equals(name)) {
            lambdaQueryWrapper.like(Storage::getName, name);
        }

        IPage<Storage> result = storageService.diyPage2(page, lambdaQueryWrapper);
        return Result.success(result.getRecords(), result.getTotal());

    }

    @PostMapping("/select")
    public List<Storage> select(@RequestBody Storage storage) {
        LambdaQueryWrapper<Storage> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(storage.getName()) && !"null".equals(storage.getName())) {
            queryWrapper.like(Storage::getName, storage.getName());
        }
        return storageService.list(queryWrapper);
    }


    @PostMapping("/save")
    public Result save(@RequestBody Storage storage) {
        return storageService.save(storage) ? Result.success() : Result.fail();
    }

    @PostMapping("/update")
    public Result update(@RequestBody Storage storage) {
        return storageService.updateById(storage) ? Result.success() : Result.fail();
    }

    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody Storage storage) {
        return storageService.saveOrUpdate(storage) ? Result.success() : Result.fail();
    }

    @DeleteMapping("/del")
    public Result del(@RequestParam int id) {
        return storageService.removeById(id) ? Result.success() : Result.fail();
    }
}
